package Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2024-02-02
 * Time: 15:25
 */
class MyCircularQueue {

    private int[] array;
    private int head;
    private int tail;

    public MyCircularQueue(int k) {
        array = new int[k];
    }

    public boolean enQueue(int value) {
        // 入队列
        if (isFull()) {
            return false;
        }
        array[tail] = value;
        tail = (tail + 1) % array.length;
        return true;
    }

    public boolean deQueue() {
        // 出队列
        if (isEmpty()) {
            return false;
        }

        head = (head + 1) % array.length;
        return true;
    }

    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return array[head];
    }

    public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        int index = tail == 0 ? array.length - 1 : tail - 1;
        return array[index];
    }

    public boolean isEmpty() {
        return head == tail;
    }

    public boolean isFull() {
        return (tail + 1) % this.array.length == head;
    }
}